<?php
/**
 * Created by PhpStorm.
 * FileName: Appointment.class.php
 * User: Administrator
 * Date: 2018/4/16
 * Time: 10:02
 */

namespace Api\Controller;

use Admin\Model\TripFavoriteModel;

class TripFavoriteController extends ApiController {

    private $tripFavoriteModel;

    public function __construct() {
        parent::__construct();
        $this->tripFavoriteModel = new TripFavoriteModel();
    }

    // 我的行程收藏
    public function getList() {
        $where = array();
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $where['user_id'] = $user_id;
        $pageSize = 15;
        $curPage = I('p',1);
        $data = $this->tripFavoriteModel->getList($where,$pageSize,$curPage);
        return $this->callBack("行程收藏",$this->SUCCESS_CODE,$data);
    }

    // 添加行程收藏
    public function add() {
        $data = array();
        $user_id = I('user_id');
        $trip_id = I('trip_id');
        if (!$user_id || !$trip_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $data = array(
            'user_id' => $user_id,
            'trip_id' => $trip_id
        );
        $isFavorited = $this->tripFavoriteModel->where($data)->count();
        if ($isFavorited > 0) {
            return $this->callBack("该行程您已收藏过！",$this->ERROR_CODE);
        }
        $rs = $this->tripFavoriteModel->add($data);
        if ($rs !== false) {
            return $this->callBack("行程收藏成功！",$this->SUCCESS_CODE);
        }
        return $this->callBack("行程收藏失败！",$this->ERROR_CODE);
    }

    // 删除行程收藏
    public function delete() {
        $user_id = I('user_id');
        $trip_id = I('trip_id');
        if (!$user_id || !$trip_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $where = array(
            'user_id' => $user_id,
            'trip_id' => $trip_id
        );
        $rs = $this->tripFavoriteModel->remove($where);
        if ($rs !== false) {
            return $this->callBack("行程收藏删除成功！",$this->SUCCESS_CODE);
        }
        return $this->callBack("行程收藏删除失败！",$this->ERROR_CODE);
    }

}